package com.ddxcloud.movie.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ddxcloud.common.annotation.RepeatSubmit;
import com.ddxcloud.common.utils.Result;
import com.ddxcloud.movie.data.dto.*;
import com.ddxcloud.movie.data.entity.Order;
import com.ddxcloud.movie.service.OrderService;
import com.ddxcloud.movie.data.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/order")
@Api(tags = "订单")
public class OrderController {

    @Resource
    OrderService orderService;

    /**
     * 提交订单
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RepeatSubmit
    @Operation(summary = "提交订单")
    @PostMapping("/addOrder")
    public Result<Object> createOrder(
            @Validated @RequestBody CreateOrderDTO dto,
            BindingResult bindingResult,
            HttpServletRequest servletRequest
    ) throws ExecutionException, InterruptedException {
        if (bindingResult.hasErrors()) {
            String errorMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            return Result.fail(errorMsg);
        }
        Order order = orderService.createOrder(dto, servletRequest).get();
        if (ObjectUtil.isNotEmpty(order)) {
            return Result.success(order);
        }
        return Result.fail();
    }

    /**
     * 确认订单
     * @param dto
     * @return
     */
    @RepeatSubmit
    @Operation(summary = "确认订单", description = "适用于免支付的环节")
    @PostMapping("/confirmOrder")
    public Result<Boolean> confirmOrder(@RequestBody ConfirmOrderDTO dto) {
        return Result.success(orderService.confirmOrder(dto));
    }

    /**
     * 订单异步通知
     * @param dto
     * @param request
     * @return
     */
    @Operation(summary = "订单异步通知", hidden = true)
    @PostMapping("/notify")
    public String notify(@RequestBody OrderDetailsDTO dto, HttpServletRequest request) {
        return orderService.notify(dto, request);
    }

    /**
     * 订单列表
     * @param dto
     * @return
     */
    @Operation(summary = "订单列表")
    @GetMapping("/getOrderList")
    public Result<IPage<OrderVO>> getOrderList(PageDTO dto) {
        return Result.success(orderService.getOrderList(dto));
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @Operation(summary = "订单详情")
    @GetMapping("/getOrderInfo")
    public Result<OrderVO> getOrderInfo(Long id) {
        return Result.success(orderService.getOrderInfo(id));
    }

    /**
     * 商品详情（远程调用）
     * @param goodsId
     * @return
     */
    @GetMapping("/getGoodsInfo")
    public Result<String> getGoodsInfo(String goodsId) {
        Map<String, Object> map = new HashMap<>();
        map.put("goodsId", goodsId);
        String result = HttpRequest.post("http://openmovie.ddx24.com:9999/product/goods/getGoodsInfo")
                .body(JSONUtil.toJsonStr(map))
                .timeout(20000)
                .execute().body();
        return Result.success(result);
    }
}
